/*
 * This file is Copyright 2002-2006 Loyola University Chicago,
 * Department of Computer Science and the Emerging Technologies Laboratory.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License via
 *    http://www.apache.org/licenses/LICENSE-2.0.
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.citep.db.hibernate.sessions;

import java.sql.SQLException;
import java.util.List;
import java.util.Collection;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.citep.business.Role;
import com.citep.business.User;
import com.citep.business.sessions.*;
import com.citep.db.hibernate.*;

public class UserSessionImpl extends HibernateDaoSupport implements UserSession {

	public User getUser(int id) {
		return (User)getHibernateTemplate().load(UserDAO.class,id);
	}

	public User createUser(String firstName, String lastName, String password,
			String email, Role role) {
		
		UserDAO user = new UserDAO();
		user.setFirstName(firstName);
		user.setLastName(lastName);
		user.setPassword(password);
		user.setEmail(email);
		user.setRole(role);
		
		return createUser(user);
	}

	public User createUser(User user) {
		getHibernateTemplate().save(user);
		return user;
	}

	public void updateUser(User user) {
		getHibernateTemplate().update(user);
	}

	public void deleteUser(int id) throws InvalidRecordException {
		User user = getUser(id);
		deleteUser(user);
	}

	public void deleteUser(User user) {
		getHibernateTemplate().delete(user);
	}

	public List<User> getUsers(final int offset, final int limit) {
		return (List)getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException,SQLException {
				Query query = session.createQuery("from UserDAO");

				if(offset > 0)
					query.setFirstResult(offset);
				
				if(limit > 0)
					query.setMaxResults(limit);
					
				return  query.list();
				
			}
		});
	
	}

	public User findUser(String email, String password) {
		String [] parameters = { email,password };
		
		Collection c = getHibernateTemplate().find("from UserDAO as user where email=? and password=?",	parameters);
		
		if(c.size() < 1)
			return null;
		
		return (User)c.iterator().next();
	}

	public Role getRole(int id) {
		Role role = (Role)getHibernateTemplate().load(RoleDAO.class,id);
		return role;
	}

	public Role createRole(String name, String description, boolean enabled) {
		RoleDAO role = new RoleDAO();
		role.setName(name);
		role.setDescription(description);
		role.setEnabled(enabled);

		return createRole(role);
	}

	public Role createRole(Role role) {
		getHibernateTemplate().save(role);
		return role;
	}

	public void updateRole(Role role) {
		getHibernateTemplate().update(role);
	}

	public void deleteRole(int id) {
		Role role = getRole(id);
		deleteRole(role);
	}

	public void deleteRole(Role role) {
		getHibernateTemplate().delete(role);
	}

	public List<Role> getRoles() {
		return getHibernateTemplate().find("from RoleDAO");
	}

	public Role findRole(String name) {
		Collection c = getHibernateTemplate().find("from RoleDAO as roles where roles.name = ?", name);
		
		if(c.size() < 1)
			return null;

		return (Role)c.iterator().next();
	}

}
